home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / comms / non-internet / samba / source / smbencrypt.c < prev    next >
C/C++ Source or Header  |  1996-06-26  |  4KB  |  203 lines

  1. #ifdef SMB_PASSWD
  2. /* 
  3.    Unix SMB/Netbios implementation.
  4.    Version 1.9.
  5.    SMB parameters and setup
  6.    Copyright (C) Andrew Tridgell 1992-1995
  7.    Modified by Jeremy Allison 1995.
  8.    
  9.    This program is free software; you can redistribute it and/or modify
  10.    it under the terms of the GNU General Public License as published by
  11.    the Free Software Foundation; either version 2 of the License, or
  12.    (at your option) any later version.
  13.    
  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.    
  19.    You should have received a copy of the GNU General Public License
  20.    along with this program; if not, write to the Free Software
  21.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23.  
  24. #include "includes.h"
  25. #include "loadparm.h"
  26. #include "des.h"
  27. #include "md4.h"
  28.  
  29. extern int DEBUGLEVEL;
  30.  
  31. #ifndef uchar
  32. #define uchar unsigned char
  33. #endif
  34. #ifndef int16
  35. #define int16 unsigned short
  36. #endif
  37. #ifndef uint16
  38. #define uint16 unsigned short
  39. #endif
  40. #ifndef uint32
  41. #define uint32 unsigned int
  42. #endif
  43.  
  44. #include "byteorder.h"
  45.  
  46. void str_to_key(uchar *str,uchar *key)
  47. {
  48.   void des_set_odd_parity(des_cblock *);
  49.   int i;
  50.  
  51.   key[0] = str[0]>>1;
  52.   key[1] = ((str[0]&0x01)<<6) | (str[1]>>2);
  53.   key[2] = ((str[1]&0x03)<<5) | (str[2]>>3);
  54.   key[3] = ((str[2]&0x07)<<4) | (str[3]>>4);
  55.   key[4] = ((str[3]&0x0F)<<3) | (str[4]>>5);
  56.   key[5] = ((str[4]&0x1F)<<2) | (str[5]>>6);
  57.   key[6] = ((str[5]&0x3F)<<1) | (str[6]>>7);
  58.   key[7] = str[6]&0x7F;
  59.   for (i=0;i<8;i++) {
  60.     key[i] = (key[i]<<1);
  61.   }
  62.   des_set_odd_parity((des_cblock *)key);
  63. }
  64.  
  65. void D1(uchar *k, uchar *d, uchar *out)
  66. {
  67.   des_key_schedule ks;
  68.   des_cblock deskey;
  69.  
  70.   str_to_key(k,(uchar *)deskey);
  71.   des_set_key(deskey,ks);
  72.   des_ecb_encrypt(d, out, ks, DES_DECRYPT);
  73. }
  74.  
  75. void E1(uchar *k, uchar *d, uchar *out)
  76. {
  77.   des_key_schedule ks;
  78.   des_cblock deskey;
  79.  
  80.   str_to_key(k,(uchar *)deskey);
  81.   des_set_key(deskey,ks);
  82.   des_ecb_encrypt(d, out, ks, DES_ENCRYPT);
  83. }
  84.  
  85. void E_P16(uchar *p14,uchar *p16)
  86. {
  87.   uchar sp7[7];
  88.   /* the following constant makes us compatible with other
  89.   implementations. Note that publishing this constant does not reduce the
  90.   security of the encryption mechanism */
  91.   uchar sp8[] = {0xAA,0xD3,0xB4,0x35,0xB5,0x14,0x4,0xEE};
  92.   uchar x[8];
  93.  
  94.   memset(sp7,'\0',7);
  95.  
  96.   D1(sp7, sp8, x);
  97.   E1(p14, x, p16);
  98.   E1(p14+7, x, p16+8);
  99. }
  100.  
  101. void E_P24(uchar *p21, uchar *c8, uchar *p24)
  102. {
  103.   E1(p21, c8, p24);
  104.   E1(p21+7, c8, p24+8);
  105.   E1(p21+14, c8, p24+16);
  106. }
  107.  
  108.  
  109. /*
  110.    This implements the X/Open SMB password encryption
  111.    It takes a password, a 8 byte "crypt key" and puts 24 bytes of 
  112.    encrypted password into p24 */
  113. void SMBencrypt(uchar *passwd, uchar *c8, uchar *p24)
  114. {
  115.   uchar p14[15], p21[21];
  116.  
  117.   memset(p21,'\0',21);
  118.   memset(p14,'\0',14);
  119.   StrnCpy((char *)p14,(char *)passwd,14);
  120.  
  121.   strupper((char *)p14);
  122.   E_P16(p14, p21);
  123.   E_P24(p21, c8, p24);
  124. }
  125.  
  126. /* Routines for Windows NT MD4 Hash functions. */
  127. static int _my_wcslen(int16 *str)
  128. {
  129.     int len = 0;
  130.     while(*str++ != 0)
  131.         len++;
  132.     return len;
  133. }
  134.  
  135. /*
  136.  * Convert a string into an NT UNICODE string.
  137.  * Note that regardless of processor type 
  138.  * this must be in intel (little-endian)
  139.  * format.
  140.  */
  141.  
  142. static int _my_mbstowcs(int16 *dst, uchar *src, int len)
  143. {
  144.     int i;
  145.     int16 val;
  146.  
  147.     for(i = 0; i < len; i++) {
  148.         val = *src;
  149.         SSVAL(dst,0,val);
  150.         dst++;
  151.         src++;
  152.         if(val == 0)
  153.             break;
  154.     }
  155.     return i;
  156. }
  157.  
  158. /* 
  159.  * Creates the MD4 Hash of the users password in NT UNICODE.
  160.  */
  161.  
  162. void E_md4hash(uchar *passwd, uchar *p16)
  163. {
  164.     int i, len;
  165.     int16 wpwd[129];
  166.     MDstruct MD;
  167.  
  168.     /* Password cannot be longer than 128 characters */
  169.     len = strlen(passwd);
  170.     if(len > 128)
  171.         len = 128;
  172.     /* Password must be converted to NT unicode */
  173.     _my_mbstowcs( wpwd, passwd, len);
  174.     wpwd[len] = 0; /* Ensure string is null terminated */
  175.     /* Calculate length in bytes */
  176.     len = _my_wcslen(wpwd) * sizeof(int16);
  177.  
  178.     MDbegin(&MD);
  179.     for(i = 0; i + 64 <= len; i += 64)
  180.         MDupdate(&MD,wpwd + (i/2), 512);
  181.     MDupdate(&MD,wpwd + (i/2),(len-i)*8);
  182.     SIVAL(p16,0,MD.buffer[0]);
  183.     SIVAL(p16,4,MD.buffer[1]);
  184.     SIVAL(p16,8,MD.buffer[2]);
  185.     SIVAL(p16,12,MD.buffer[3]);
  186. }
  187.  
  188. /* Does the NT MD4 hash then des encryption. */
  189.  
  190. void SMBNTencrypt(uchar *passwd, uchar *c8, uchar *p24)
  191. {
  192.     uchar p21[21];
  193.  
  194.     memset(p21,'\0',21);
  195.  
  196.     E_md4hash(passwd, p21);    
  197.     E_P24(p21, c8, p24);
  198. }
  199.  
  200. #else
  201. void smbencrypt_dummy(void){}
  202. #endif
  203.